package com.example.course_topic.service.impl;

import com.example.model.common.R;
import com.example.model.pojo.CourseTopic;
import com.example.course_topic.mapper.TopicMapper;
import com.example.course_topic.service.TopicService;
import com.example.model.pojo.User;
import com.example.model.pojo.dto.TopicDTO;
import com.example.model.pojo.vo.TopicVO;

import com.example.model.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 题目服务实现类
 */
@Service
public class TopicServiceImpl implements TopicService {

    // 调用Mapper层,执行数据库操作
    @Autowired
    private TopicMapper topicMapper;

    @Override
    public Map<String, Object> getTopicList(int page, int size, int active) {
        // 设置默认值
        if (page < 1) {
            page = 1;
        }
        if (size < 1) {
            size = 10;
        }
        if (active < 0 || active > 1) {
            active = 1; // 默认查询激活的题目
        }

        int offset = (page - 1) * size;
        List<TopicVO> topicVOList = topicMapper.getTopicList(offset, size, active);
        int total = topicMapper.getTopicCount(active);

        // 计算总页数
        Integer pages = (total + size - 1) / size;

        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("pages", pages);
        result.put("current", page);
        result.put("records", topicVOList);

        return result;
    }

    @Override
    public TopicVO getTopicById(Integer id) {
        CourseTopic topic = topicMapper.getTopicById(id);

        if (topic == null) {
            return null;
        }

        TopicVO topicVO = new TopicVO();
        BeanUtils.copyProperties(topic, topicVO);
        return topicVO;
    }

    @Override
    public boolean createTopic(CourseTopic topic) {
        return topicMapper.createTopic(topic);
    }

    @Override
    public boolean updateTopic(Integer id, TopicDTO topicDTO) {
        // 检查题目是否存在且属于该教师
        if (!isTopicCreator(id)) {
            return false;
        }
        
        // 获取题目并更新信息
        CourseTopic existingTopic = topicMapper.getTopicById(id);
        if (existingTopic == null) {
            return false;
        }

        // 更新题目信息
        existingTopic.setTitle(topicDTO.getTitle());
        existingTopic.setDescription(topicDTO.getDescription());
        existingTopic.setMaxStudents(topicDTO.getMaxStudents());
        existingTopic.setIsActive(topicDTO.getIsActive());
        
        // 执行更新操作并返回实际结果
        return topicMapper.updateTopic(existingTopic);
    }

    @Override
    public boolean deleteTopic(Integer id) {
        // 检查题目是否存在且属于该教师
        if (!isTopicCreator(id)) {
            return false;
        }
        
        // 检查题目是否已被学生选择，如果已被选择则不允许删除
        if (isTopicSelected(id)) {
            return false;
        }
        System.out.println("删除题目：" + id);
        return topicMapper.deleteTopic(id);
    }

    //  检查是否是题目创建者(本类方法)
    @Override
    public boolean isTopicCreator(Integer topicId) {
        // 使用getTopicByTeacherAndId直接查询特定教师的题目，若存在则说明是创建者
        User currentUser = UserHolder.get();
        if (currentUser == null) {
            return false;
        }
        Integer currentTeacherId = currentUser.getId();

        CourseTopic topic = topicMapper.getTopicByTeacherAndId(topicId);
        // 添加空值检查，避免NullPointerException
        if (topic == null) {
            return false;
        }
        return Objects.equals(topic.getTeacherId(), currentTeacherId);
    }

    // 检查题目是否已被选题(本类方法)
    @Override
    public boolean isTopicSelected(Integer topicId) {
        Integer result = topicMapper.isTopicSelected(topicId);
        return result != null && result > 0;
    }
}